﻿using System;
using System.Collections.Generic;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Pages;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine.Installer
{
    public class PluginController
    {
        public static List<Plugin> GetInstalledPlugins()
        {
            List<Plugin> list = (List<Plugin>)DataCache.GetCache("PluginsInstalled");
            if (list == null)
            {
                IDataReader dr = SmartEngineDataProvider.Provider.GetInstalledPlugins();
                list = new List<Plugin>();
                while (dr.Read())
                {
                    Plugin plugin = new Plugin();
                    if (dr["PluginId"] != Convert.DBNull)
                        plugin.PluginId = (int)dr["PluginId"];
                    if (dr["Name"] != Convert.DBNull)
                        plugin.Name = (string)dr["Name"];
                    if (dr["Description"] != Convert.DBNull)
                        plugin.Description = (string)dr["Description"];
                    if (dr["UrlSeparator"] != Convert.DBNull)
                        plugin.UrlSeparator = (string)dr["UrlSeparator"];
                    if (dr["UrlRewriteController"] != Convert.DBNull)
                        plugin.UrlRewriteController = (string)dr["UrlRewriteController"];
                    if (dr["SitemapController"] != Convert.DBNull)
                        plugin.SiteMapController = (string)dr["SitemapController"];
                    if (dr["Version"] != Convert.DBNull)
                        plugin.Version = (string)dr["Version"];
                    list.Add(plugin);
                }
                dr.Close();
                DataCache.SetCache("PluginsInstalled", list);
            }
            return list;
        }

        public static Plugin GetInstalledPlugin(string Name)
        {
            return GetInstalledPlugins().Find(p => p.Name.ToLower() == Name.ToLower());
        }

        public static Plugin GetInstalledPlugin(int PluginId)
        {
            return GetInstalledPlugins().Find(p => p.PluginId == PluginId);
        }

        public static void AddPlugin(Plugin plugin)
        {
            // don't install if already registered
            if (GetInstalledPlugins().Exists(p => string.Compare(p.Name, plugin.Name, true) == 0))
                return;
            IDataReader dr = SmartEngineDataProvider.Provider.AddPlugin(plugin.Name, plugin.Description, plugin.UrlSeparator, plugin.UrlRewriteController, plugin.SiteMapController, plugin.Version);
            //DataCache.DeleteCacheMultiple("PluginsInstalled");
            DataCache.ClearCache();
        }   

        public static void UpdatePlugin(Plugin plugin)
        {
            IDataReader dr = SmartEngineDataProvider.Provider.UpdatePlugin(plugin.PluginId, plugin.Name, plugin.Description, plugin.UrlSeparator, plugin.UrlRewriteController, plugin.SiteMapController, plugin.Version);
            //DataCache.DeleteCacheMultiple("PluginsInstalled");
            DataCache.ClearCache();
        }

        /// <summary>
        /// Deletes the plugin and also removes all the pages associated with the plugin
        /// </summary>        
        public static void DeletePlugin(int PluginId)
        {
            // remove all pages associated with the plugin
            // this will even delete all modules on the specified pages.
            foreach (PageInfo page in PageController.GetPages(PluginId))
                PageController.DeletePage(page.PageId);
            SmartEngineDataProvider.Provider.DeletePlugin(PluginId);
            //DataCache.DeleteCacheMultiple("PluginsInstalled");
            DataCache.ClearCache();
        }
    }
}
